package org.baicaix.game.warworms {
	import com.worms.map.BattleMap;

	import org.baicaix.game.warworms.Battle.Battle;
	import org.baicaix.game.warworms.Battle.Camera;
	import org.baicaix.game.warworms.worm.Worm;
	import org.baicaix.io.controll.Command;
	import org.baicaix.io.controll.Keyboard;
	import org.baicaix.io.controll.KeyboardManager;

	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;

	/**
	 * @author baicaix
	 */
	public class Worms extends Sprite {
		
		private var worms : Array;
		private var worm : Worm;

		public function Worms() {
			initStage();
			
			var battle : Battle = addChild(new Battle()) as Battle;
			battle.changeMap(BattleMap.getInstance());
			
			worms.push(new Worm("aaa"));
			worms.push(new Worm("bbb"));
			worms.push(new Worm("ccc"));
			worms.push(new Worm("ddd"));
			worms.push(new Worm("eee"));
			worms.push(new Worm("fff"));
			
			for each (var worm : Worm in worms) {
				worm
			}
			
			worm = worms.shift();
			initKeyBoard();
			Camera.getInstance().init(stage.fullScreenWidth, stage.fullScreenHeight);
			Camera.getInstance().moveTo(worm.x, worm.y);
			
			nextWorm();
		}
		
		private function initStage() : void {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
		}
		
		private function initKeyBoard() : void {
			KeyboardManager.getInstance().init(stage);
			registerKeyboard();
		}

		private function registerKeyboard() : void {
			var upCommand 	: Command 	= new Command(worm, worm.weapon.rotate, -4);
			var downCommand : Command 	= new Command(worm, worm.weapon.rotate, 4);
			var leftCommand : Command 	= new Command(worm, worm.move, -2, 0);
			var rightCommand : Command 	= new Command(worm, worm.move, 2, 0);
			
			KeyboardManager.registerKeyCommand(Keyboard.W, upCommand);
			KeyboardManager.registerKeyCommand(Keyboard.S, downCommand);
			KeyboardManager.registerKeyCommand(Keyboard.A, leftCommand);
			KeyboardManager.registerKeyCommand(Keyboard.D, rightCommand);
			
			var fireCommand : Command 	= new Command(worm, worm.weapon.fire);
			var holdCommand : Command 	= new Command(worm, worm.weapon.hold);
			
			KeyboardManager.registerKeyCommand(Keyboard.SPACE, holdCommand, fireCommand);
		}

		private function unregisterKeyboard() : void {
			var upCommand 	: Command 	= new Command(worm, worm.weapon.rotate, -4);
			var downCommand : Command 	= new Command(worm, worm.weapon.rotate, 4);
			var leftCommand : Command 	= new Command(worm, worm.move, -2, 0);
			var rightCommand : Command 	= new Command(worm, worm.move, 2, 0);
			
			KeyboardManager.unregisterKeyCommand(Keyboard.W, upCommand);
			KeyboardManager.unregisterKeyCommand(Keyboard.S, downCommand);
			KeyboardManager.unregisterKeyCommand(Keyboard.A, leftCommand);
			KeyboardManager.unregisterKeyCommand(Keyboard.D, rightCommand);
			
			var fireCommand : Command 	= new Command(worm, worm.weapon.fire);
			var holdCommand : Command 	= new Command(worm, worm.weapon.hold);
			
			KeyboardManager.unregisterKeyCommand(Keyboard.SPACE, holdCommand, fireCommand);
		}

		private function nextWorm() : void {
			unregisterKeyboard();
			worms.push(worm);
			worms.shift();
			registerKeyboard();
			Camera.getInstance().moveTo(worm.x, worm.y);
		}
		
//		private function initMouseAction() : void {
//			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
//			stage.addEventListener(MouseEvent.CLICK, mouseClickHandler);
//		}


//		private function mouseMoveHandler(event : MouseEvent) : void {
//			var radians : Number = Math.atan2(event.stageY - weapon.y, event.stageX - weapon.x);
//			weapon.rotation = RadiansUtil.radiansToAngle(radians);
//		}
//
//		private function mouseClickHandler(event : MouseEvent) : void {
//			weapon.fire();			
//		}
	}
}
